package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MemberService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/8/5 10:52
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {


	@Reference
	UserService userService;


	/**
	 *  @描述：获取用户名
	 */
	@RequestMapping("/getUserName")
	public Result getUserName() {
		try {
			User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
		} catch(Exception e){
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_USERNAME_FAIL);
		}
	}

	/**
	 *通过用户名得到对应的菜单
	 * @return
	 */
	@RequestMapping("getMenu")
	public Result getMenu(){
		String username = (String) getUserName().getData();
		try {
			List<Menu> list = userService.findMenuByUsername(username);
			return new Result(true,MessageConstant.GET_MENU_SUCCESS,list);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.GET_MENU_FAIL);
		}
	}
}
